package com.cssy.yd.logic.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BaseController;
import com.cssy.yd.logic.entity.LogicPriceDetailsDO;
import com.cssy.yd.logic.entity.LogicWhAffstockDO;
import com.cssy.yd.logic.service.ILogicWhAffStockService;
import com.cssy.yd.logic.util.SqlUtil;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.ExcelUtil;
import com.plat.util.json.OutJsonGrid;

@Controller
@RequestMapping("/yd/logic/whaffstockGather")
public class WhaffstockGatherController extends BaseController<LogicWhAffstockDO>{
	private static final String MANAGER = "/yd/logic/stockgather/StockGatherManager";
	
	@Resource(name="logicWhAffStockService")
	private ILogicWhAffStockService logicWhAffStockService;
	
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander=new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		Map<String,Object> sqlMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService,"whCode");
		scs.addCondition((String)sqlMap.get("option"), (List)sqlMap.get("roleWhList"));
		scs.setRemovedProperty(null);
		scs.setLimitPage(false);
		SearchResult rs = logicWhAffStockService.stockGather(scs);
		OutJsonGrid out = new OutJsonGrid(rs);
		out.printJSon(response);
		
	}

	@Override
	public String operationForm(HttpServletRequest request, String id,
			String flag, Model model) throws Exception {
		return null;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicWhAffstockDO curAP, BindingResult result, Model model)
			throws Exception {
		return null;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		return null;
	}

	/**
	 * 库存汇总导出
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/exportExcel")
	public void exportExcel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		scs.clearCondition();
		hander.setFilters(new String(request.getParameter("filters").getBytes(
				"ISO-8859-1"), "UTF-8")); // 转码
		hander.getWheres(scs);
		scs.setLimitClause(null);
		scs.setRemovedProperty(null);
		Map<String,Object> sqlMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService,"whCode");
		scs.addCondition((String)sqlMap.get("option"), (List)sqlMap.get("roleWhList"));
		SearchResult rs = logicWhAffStockService.stockGather(scs);
		List<LogicWhAffstockDO>  list = rs.getResults();
		ExcelUtil.toExcel(list, "仓库编码:whCode┋仓库:whname┋物料编号:code┋物料名称:name┋型号:spec┋单位:unit┋一等品总数量:ydengquantity┋次品总数量:quantity1┋旧件总数量:quantity2┋冻结总数量:hgquantity┋在途总数量:bhgquantity┋总库存数量:quantity6","库存汇总",response);
	}
}
